<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class recharge extends core {
	private $recharge_mod;

	public function __construct() {
		$this->recharge_mod = m("recharge");
		parent::__construct();
	}

	//列表
	public function index() {
		$where = '';
		//分页
		$this->ezr->num_results_per_page = 100;
		$sql = "SELECT r.* FROM jf_recharge r WHERE 1 {$where} ORDER BY r.id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]['status_name'] = $this->recharge_mod->status_name($row['status']);
			}
		}
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->display();
	}

	//明细
	public function history() {
		$where = '';
		$status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($keyword) {
			$where .= "AND (mr.order_sn LIKE '%{$keyword}%' OR mr.pay_method LIKE '%{$keyword}%' OR m.name LIKE '%{$keyword}%')";
		}
		if (strlen($status)) {
			$where .= "AND mr.status='{$status}'";
		}
		//分页
		$this->setpages(array('keyword'=>$keyword,  'status'=>$status));
		$sql = "SELECT mr.*, m.name as member_name 
			FROM jf_member_recharge mr LEFT JOIN im_member m ON mr.member_id=m.id 
			WHERE 1 {$where} 
			ORDER BY mr.id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				//$rs[$key]['status_name'] = $this->project_mod->status_name($row['status']);
			}
		}
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('status',$status);
		$this->smarty->assign('page',$nav);
		$this->display();
	}


	//add
	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$this->display();
		}
	}	

	//编辑
	public function edit() {
		if (IS_POST) {
			$this->save();
		} else {
			$recharge_id = (isset($_GET['recharge_id']) && intval($_GET['recharge_id'])) ? intval($_GET['recharge_id']) : 0;
			$sql = "SELECT * FROM jf_recharge WHERE id='{$recharge_id}'";
			$recharge = $this->db->get_row($sql);
			$this->smarty->assign('row', $recharge);
			$this->display();
		}
	}

	//保存
	public function save() {
		$recharge_id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$price = (isset($_POST['price']) && floatval($_POST['price'])) ? floatval($_POST['price']) : 0;
		$recharged_money = (isset($_POST['recharged_money']) && floatval($_POST['recharged_money'])) ? floatval($_POST['recharged_money']) : 0;
		$promotion_begin_time = (isset($_POST['promotion_begin_time']) && trim($_POST['promotion_begin_time'])) ? trim($_POST['promotion_begin_time']) : '';
		$promotion_end_time = (isset($_POST['promotion_end_time']) && trim($_POST['promotion_end_time'])) ? trim($_POST['promotion_end_time']) : '';
		$promotion_recharged_money = (isset($_POST['promotion_recharged_money']) && floatval($_POST['promotion_recharged_money'])) ? floatval($_POST['promotion_recharged_money']) : 0;
		$status = (isset($_POST['status']) && trim($_POST['status'])) ? intval($_POST['status']) : 0;
		if ($promotion_begin_time) $promotion_begin_time=strtotime($promotion_begin_time);
		if ($promotion_end_time) $promotion_end_time=strtotime($promotion_end_time);
		//图片
		$pic = upload_one_file('recharge', 'pic');
		if (!$pic) {
			$pic = (isset($_POST['origin_pic']) && trim($_POST['origin_pic'])) ? trim($_POST['origin_pic']) : '';
		}
		if ($recharge_id) {
			$sql = "UPDATE jf_recharge SET name='{$name}', pic='{$pic}', 
					price='{$price}', recharged_money='{$recharged_money}', promotion_begin_time='{$promotion_begin_time}',
					promotion_end_time='{$promotion_end_time}', promotion_recharged_money='{$promotion_recharged_money}', status='{$status}' 
					WHERE id='{$recharge_id}' ";
			$this->db->query($sql);
		} else {
			$sql = "INSERT INTO jf_recharge(name, pic, price, recharged_money, promotion_begin_time, 
					promotion_end_time, promotion_recharged_money, status) VALUES(
					'{$name}', '{$pic}', '{$price}', '{$recharged_money}', '{$promotion_begin_time}', 
					'{$promotion_end_time}', '{$promotion_recharged_money}', '{$status}')";
			$this->db->query($sql);
			$recharge_id = $this->db->insert_id;
		}
		//echo $sql;
		//exit;
		header("Location:?app=recharge&act=index");
	}

	//保存
	public function manual_recharge() {
		if (IS_POST) {
			$member_id = (isset($_POST['member_id']) && intval($_POST['member_id'])) ? intval($_POST['member_id']) : 0;
			$price = (isset($_POST['price']) && floatval($_POST['price'])) ? floatval($_POST['price']) : 0;
			$bonus = (isset($_POST['bonus']) && floatval($_POST['bonus'])) ? floatval($_POST['bonus']) : 0;
			$recharged_money = $price + $bonus;
			$order_sn = "CZ".generate_order_sn();
			if ($member_id) {
				$sql = "INSERT INTO jf_member_recharge(member_id, recharge_id, price, recharged_money, bonus, 
					add_time, expired_time, status, order_sn, pay_method, paid_time, recharge_type) 
					VALUES('{$member_id}', '0', '{$price}', '{$recharged_money}', '{$bonus}', 
					'".time()."', '0', '1', '{$order_sn}', 'manual', '".time()."', '2')";
				$this->db->query($sql);
				$sql = "UPDATE jf_member SET money=money+{$recharged_money} WHERE id='{$member_id}'";
				$this->db->query($sql);
			}
			header("Location:?app=recharge&act=history");
		} else {
			$member_id = (isset($_GET['member_id']) && intval($_GET['member_id'])) ? intval($_GET['member_id']) : 0;
			$this->smarty->assign("members", $this->_members());
			$this->smarty->assign("member_id", $member_id);
			$this->display();
		}
	}

	//删除
	public function delete() {
		$recharge_id = (isset($_GET['recharge_id']) && trim($_GET['recharge_id'])) ? intval($_GET['recharge_id']) : 0;
		$sql = "DELETE FROM jf_recharge WHERE id='{$recharge_id}'";
		$this->db->query($sql);
		header("Location:?app=recharge&act=index");
	}

	private function _members() {
		$sql = "SELECT id, name, mobile FROM jf_member WHERE status=1 ORDER BY name ASC";
		$members = $this->db->get_results($sql);
		return $members;
	}
}
